import {Component, OnInit} from '@angular/core';
import {NewsService} from './news.service';
import {StatusEnum} from '../../../core/enum/status.enum';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {FilterEnum, FilterTypeEnum} from '../../../core/enum/filter.enum';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {PageEntity} from '../../../core/domain/page.entity';
import {OtherUtil} from '../../../utils/other.util';
import {AppReuseStrategy} from '../../../core/AppReuseStrategy';

@Component({
  selector: 'app-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.scss'],
  providers: [NewsService]
})
export class NewsComponent implements OnInit {

  isModalSend = false;
  pageInfo: PageEntity = new PageEntity();
  messType: number = 1;
  list: Array<any> = [];// 消息列表
  isCkecked: boolean = false;
  counts = {
    approveCount: 0,
    otherCount: 0,
    systemCount: 0,
    topicCount: 0
  };
  hasSend: boolean = false;
  searchName: string = '';
  isShowModalContent: boolean = false;
  addNews: SendInfoParam = new SendInfoParam(); // 发布消息实体参数
  searchCodeParam = {
    name: '',
    code: ''
  };
  // 查看消息
  isShowDetail: boolean = false;
  // 查看消息数据实体
  lookNews: LookView = new LookView();
  // 老师
  teacherList: Array<any> = [];
  teacherPageInfo: PageEntity = new PageEntity();
  displayList: Array<any> = [];
  mapOfCheckedId: { [key: string]: boolean } = {};// 发布消息弹窗自定义选择老师id容器
  mapOfInfoCheckedId: { [keyL: string]: boolean } = {};// 消息页面id容器
  isAllCheck: boolean = false;
  isIndeterminate: boolean = false;
  infoStateList: Array<{ key: string, value: string }> = [{
    key: '',
    value: '全部'
  },
    {
      key: '0',
      value: '未读'
    },
    {
      key: '1',
      value: '已读'
    }];// 信息状态选择列表
  stateSelValue: string = '';// 选中值
  // 表格上的过滤
  filterSexArr: Array<{ text: string, value: number }> = [
    {
      text: '男',
      value: 0
    },
    {
      text: '女',
      value: 1
    }
  ];
  filterTypeArr: Array<{ text: string; value: number }> = [
    {
      text: '专技',
      value: 0
    },
    {
      text: '工勤',
      value: 1
    },
    {
      text: '管理',
      value: 2
    }
  ];
  filterPositionArr: Array<any> = [];
  filterTitleArr: Array<any> = [];
  filterWorkTypeArr: Array<any> = [];
  searchDepart: any = null;
  canDelete: boolean = true;
  // 学生
  studentList: Array<any> = [];
  studentPageInfo: PageEntity = new PageEntity();
  studentDisplayList: Array<any> = [];
  studentMapOfCheckedId: { [key: string]: boolean } = {};
  studentIsAllCheck: boolean = false;
  studentIsIndeterminate: boolean = false;
  // 学生类型
  filterTypeArrStudent: Array<{ text: string; value: number }> = [
    {
      text: '本科生',
      value: 0
    },
    {
      text: '研究生',
      value: 1
    },
    {
      text: '博士生',
      value: 2
    }
  ];
  filterCollegeArr: Array<any> = [];
  // 学院/专业树结构数据
  treeNodes: Array<any> = [];
  classList: Array<any> = [];
  classPageInfo: PageEntity = new PageEntity();
  classDisplayList: Array<any> = [];
  classMapOfCheckedId: { [key: string]: boolean } = {};
  classIsAllCheck: boolean = false;
  classIsIndeterminate: boolean = false;
  // 当前点击的事件何种类型的数据
  currentType: string = '';
  majorId: string = '';
  className: string = '';
  // 选择的name
  checkNames: string = '';
  // 定时器
  timers;
  infoText: string | any;// 消息富文本内容
  private subId: string;// 单个查看id

  constructor(private newsService: NewsService,
              private message: NzMessageService,
              private router: Router) {
  }

  ngOnInit() {
    this.canSend();// 校验发布权限
    // this.count();// 消息分类统计
    this.messType = 0;// 默认读取率全部消息
    this.page(this.pageInfo);
  }

  /**
   * 消息分页函数查询
   * @param page 分页条件
   */
  page(page: PageEntity): void {
    this.newsService.page(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        if ((res.datas && res.datas.page) || (res.datas && res.datas.count)) {
          this.list = res.datas.page.data;
          const {approveCount, systemCount, otherCount} = res.datas.count;
          this.counts = {
            approveCount: approveCount,
            otherCount: otherCount,
            systemCount: systemCount,
            topicCount: (approveCount + otherCount + systemCount)
          };
          this.pageInfo.totalRecords = res.datas.page.totalRecords;
        }
      }
    });
  }

  selFn(event: any): void {
    this.stateSelValue = event;
    if (event !== '') {
      this.dealPageLogic(this.pageInfo, 'hasRead', this.stateSelValue, FilterEnum.EQUALS, FilterTypeEnum.AND);
    } else {
      this.pageInfo = new PageEntity();
      this.dealPageLogic(this.pageInfo, 'messType', this.messType, FilterEnum.EQUALS, FilterTypeEnum.AND);
    }
    this.page(this.pageInfo);
  }

  /**
   * 查询是否有发布消息权限
   */
  canSend(): void {
    this.newsService.canSend().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.hasSend = res.datas;
      }
    });
  }

  /**
   * tab点击获取分页查询数据
   * @param num 值
   */
  tabClick(num): void {
    this.searchName = '';
    this.messType = num;
    this.stateSelValue = '';
    this.mapOfInfoCheckedId = {};
    this.isCkecked = false;
    this.pageInfo = new PageEntity();
    if (num !== 0) {
      this.pageInfo.filters['messType'.toString()] = PaginationUtils.filters(this.messType, FilterEnum.EQUALS);
    }
    this.page(this.pageInfo);
  }

  /**
   * 点击跳转对应的模块页面
   * @param data:数据源
   */
  clickJump(data: any): void {
    const keyStr: string = data.redirectUrl;
    let toUrl: string = '';
    let routerKey: string = '';
    let subscribeId: string = data.targetId;// 跳转预约模块所需id
    let useType: string = '1'; // 预约类型 1--个人，2-团队
    switch (keyStr) {
      case 'personal_subscribe_view':
        toUrl = `/pages/appointment-system/appointment-record`;
        useType = '1';// 个人
        this.dealSubscribeJump(toUrl, subscribeId, useType);
        break;
      case 'team_subscribe_view':
        toUrl = `/pages/appointment-system/appointment-record`;
        useType = '2';// 团队
        this.dealSubscribeJump(toUrl, subscribeId, useType);
        break;
      case 'tch_safety_exam':
        toUrl = `/pages/safety-education/safety-examination`;
        routerKey = '1';
        this.dealDefaultJump(toUrl, routerKey);
        break;
      case 'tch_safety_learn_task':
        toUrl = `/pages/safety-education/learn-tasks`;
        routerKey = '2'; // 消息中心跳转标识
        this.dealDefaultJump(toUrl, routerKey);
        break;
      case 'tch_classroom_learn_task':
        toUrl = `/pages/safety-education/learn-tasks`;
        routerKey = '2'; // 消息中心跳转标识
        this.dealDefaultJump(toUrl, routerKey);
        break;
      case 'tch_ask_leave_approve':
        console.log('前往审批到请假管理');
        this.message.warning('请假管理系统暂未开放,不能跳转');
        break;
      case 'tch_ask_leave_view':
        this.message.warning('请假管理系统暂未开放,不能跳转');
        console.log('查看请假-到请假管理');
        break;
      case 'tch_repeal_approve':
        toUrl = `/pages/appointment-system/approval`;
        routerKey = '2';// 撤销审批标识
        this.dealDefaultJump(toUrl, routerKey);
        break;
      case 'tch_subscribe_approve':
        toUrl = `/pages/appointment-system/approval`;
        routerKey = '1';// 预约审批标识
        this.dealDefaultJump(toUrl, routerKey);
        break;
      case 'tch_break_promise_personal_view':
        toUrl = `/pages/appointment-system/default-record`;
        routerKey = '1';// 个人
        this.dealDefaultJump(toUrl, routerKey);
        break;
      case 'tch_break_promise_team_view':
        toUrl = `/pages/appointment-system/default-record`;
        routerKey = '2'; // 团队
        this.dealDefaultJump(toUrl, routerKey);
        break;
      case 'stu_break_promise_personal_view':
        toUrl = `/pages/appointment-system/default-record`;
        routerKey = '1'; // 团队
        this.dealDefaultJump(toUrl,routerKey);
        break;
      case 'stu_break_promise_team_view':
        toUrl = `/pages/appointment-system/default-record`;
        routerKey = '2'; // 团队
        this.dealDefaultJump(toUrl,routerKey);
        break;
      default:
        break;

    }
  }

  /**
   * 简单模块跳转
   * @param toUrl:targetUrl
   * @param routerKey:key
   */
  dealDefaultJump(toUrl: string, routerKey: string): void {
    AppReuseStrategy.deleteRouteSnapshot(toUrl);
    // 携带控制参数跳转
    this.router.navigate([toUrl], {
      queryParams: {
        homeId: routerKey
      }
    });
  }

  /**
   * 消息点击跳转预约模块详情处理函数
   * @param toUrl:targetUrl
   * @param subscribeId:targetId
   * @param useType:subscribeType
   */
  dealSubscribeJump(toUrl: string, subscribeId: string, useType: string): void {
    AppReuseStrategy.deleteRouteSnapshot(toUrl);
    // 携带控制参数跳转
    this.router.navigate([toUrl], {
      queryParams: {
        id: subscribeId,
        useType: useType,
      }
    });
  }

  /**
   * 点击查看消息内容弹框
   * @param data:数据
   */
  newsClick(data: any): void {
    this.subId = data.id;
    this.isShowDetail = true;
    this.lookNews = {
      senderName: data.senderName,
      content: data.content,
      gmtCreate: data.gmtCreate,
      title: data.title,
      hasRead: data.hasRead
    };
  }

  closeFn(strUrl: string) {
    this.isShowDetail = false;
    if (this.lookNews.hasRead === 0) {
      this.dealHasRead([this.subId], strUrl);
    }
  }

  /**
   * checkbox
   */
  check(): void {
    this.isCkecked = false;
    this.list.forEach(item => {
      if (item.checked === true) {
        this.isCkecked = true;
      }
    });
  }

  /**
   * 标记已读-未读
   * @param urlStr：signRead：已读，signUnread-未读
   */
  signRead(urlStr: string): void {
    const selArr: Array<string> = this.dealArrLogic();
    this.dealHasRead(selArr, urlStr);
  }

  dealHasRead(selArr: Array<string>, urlStr: string): void {
    this.newsService.signReadList(selArr, urlStr).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        // this.message.success(res.message);
        this.mapOfInfoCheckedId = {};
        this.page(this.pageInfo);
        this.isCkecked = false;
      }
    });
  }

  /**
   * 数组逻辑处理
   */
  dealArrLogic(): Array<string> {
    const arrId: Array<string> = [];
    for (const val in this.mapOfInfoCheckedId) {
      if (this.mapOfInfoCheckedId[val]) {
        arrId[arrId.length] = val;
      }
    }
    return arrId;
  }

  /**
   * 标记全部已读
   */
  allRead(): void {
    let obj: { [key: string]: number } = {
      messType: this.messType
    };
    this.newsService.readAll(obj).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.page(this.pageInfo);
        this.isCkecked = false;
      }
    });
  }

  /**
   * 鼠标回车查询
   * @param event:当前键值
   */
  search(event): void {
    if (event.keyCode === 13) {
      this.searchClick();
    }
  }

  /**
   * 模糊查询
   */
  searchClick(): void {
    this.pageInfo.first = 1;
    const strArr = ['senderName', 'content', 'receiverName'];
    strArr.forEach((item) => {
      this.dealPageLogic(this.pageInfo, item, this.searchName, FilterEnum.CONTAINS, FilterTypeEnum.OR);
    });
    this.dealPageLogic(this.pageInfo, 'hasRead', this.stateSelValue, FilterEnum.EQUALS, FilterTypeEnum.AND);
    this.page(this.pageInfo);
  }

  /**
   * 发布消息
   */
  newSend(): void {
    this.isModalSend = true;
    this.isShowModalContent = false;
    this.addNews = new SendInfoParam();
    this.addNews.customType = 1;
    this.addNews.receiverType = '1';
    this.addNews.eject = '1';
    this.addNews.grade = '0';
    this.checkNames = '';
  }

  /**
   * 点击弹出发布消息选择框
   */
  choose(): void {
    this.isShowModalContent = true;
    this.addNews.customType = 1;
    this.searchMore();
    this.tabMoadalClick(1); // 初始选择老师
  }

  /**
   * 分页查询-教师
   * @param page 分页条件
   */
  pageTeacher(page: PageEntity): void {
    this.newsService.teacherPage(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.teacherList = res.datas.data;
        this.teacherPageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  /**
   * 表格过滤
   */
  filterSome(event, flag): void {
    if (event.length > 0) {
      this.teacherPageInfo.first = 1;
      this.teacherPageInfo.filters[flag] = {
        matchMode: FilterEnum.IN,
        value: event.join(','),
        whereType: FilterTypeEnum.AND
      };
    } else {
      this.teacherPageInfo.filters[flag] = undefined;
    }
    this.pageTeacher(this.teacherPageInfo);
  }

  /**
   * 查询条件过滤复用逻辑
   * @param pageConfig:当前分页对象
   * @param filterVal:当前过后台接受的过滤值
   * @param curVal:当前条件值
   * @param mode:模式
   * @param type:查询模式
   */
  dealPageLogic(pageConfig: PageEntity, filterVal: any, curVal: any, mode: any, type: any): void {
    curVal ? pageConfig.filters[filterVal.toString()] = {
      matchMode: mode,
      value: curVal,
      whereType: type
    } : pageConfig.filters[filterVal.toString()] = undefined;

  }

  /**
   * 点击查询
   * @param num：1-老师查询，2-学生查询
   */
  clickSearch(num): void {
    if (num === 1) {
      this.teacherPageInfo.first = 1;
      this.dealPageLogic(this.teacherPageInfo, 'name', this.searchCodeParam.name, FilterEnum.CONTAINS, FilterTypeEnum.AND);
      this.dealPageLogic(this.teacherPageInfo, 'number', this.searchCodeParam.code, FilterEnum.CONTAINS, FilterTypeEnum.AND);
      this.pageTeacher(this.teacherPageInfo);
    } else {
      this.studentPageInfo.first = 1;
      this.dealPageLogic(this.studentPageInfo, 'name', this.searchCodeParam.name, FilterEnum.CONTAINS, FilterTypeEnum.AND);
      this.dealPageLogic(this.studentPageInfo, 'number', this.searchCodeParam.code, FilterEnum.CONTAINS, FilterTypeEnum.AND);
      this.pageStudent(this.studentPageInfo);
    }
  }

  /**
   * 点击重置
   * @param num：1-老师重置，2-学生重置
   */
  resetSearch(num): void {
    this.searchCodeParam.name = '';
    this.searchCodeParam.code = '';
    if (num === 1) {
      this.teacherPageInfo.first = 1;
      this.teacherPageInfo.filters['number'.toString()] = undefined;
      this.teacherPageInfo.filters['name'.toString()] = undefined;
      this.pageTeacher(this.teacherPageInfo);
    } else {
      this.studentPageInfo.first = 1;
      this.studentPageInfo.filters['number'.toString()] = undefined;
      this.studentPageInfo.filters['name'.toString()] = undefined;
      this.pageStudent(this.studentPageInfo);
    }
  }

  /**
   * 部门搜索
   */
  searchByDepart(): void {
    this.teacherPageInfo.first = 1;
    this.dealPageLogic(this.teacherPageInfo, 'orgName', this.searchDepart, FilterEnum.CONTAINS, FilterTypeEnum.AND);
    this.pageTeacher(this.teacherPageInfo);
  }

  /**
   * 部门搜索重置
   */
  resetDepart(): void {
    this.searchDepart = null;
    this.teacherPageInfo.first = 1;
    this.teacherPageInfo.filters['orgName'.toString()] = undefined;
    this.pageTeacher(this.teacherPageInfo);
  }

  /**
   * 查询过滤的职务/职称/任职类型
   */
  async searchMore() {
    let paramArr: Array<{ dataArr: Array<any>, val: string }> = [];
    paramArr = [
      {val: 'teacher-position', dataArr: this.filterPositionArr},
      {val: 'teacher-title', dataArr: this.filterTitleArr},
      {val: 'teacher-workType', dataArr: this.filterWorkTypeArr}];
    // @ts-ignore
    this.filterPositionArr = await this.dealHttpLogic(paramArr[0].val, paramArr[0].dataArr);
    // @ts-ignore
    this.filterTitleArr = await this.dealHttpLogic(paramArr[1].val, paramArr[1].dataArr);
    // @ts-ignore
    this.filterWorkTypeArr = await this.dealHttpLogic(paramArr[2].val, paramArr[2].dataArr);
  }

  /**
   *  复用接口请求逻辑，异步->同步
   * @param resVal:str 值
   * @param dataConfig:配置数组
   */
  dealHttpLogic(resVal: string, dataConfig: Array<any>) {
    return new Promise((resolve, reject) => {
      this.newsService.threeList(resVal).subscribe(res => {
        if (res.status === StatusEnum.SUCCESS) {
          res.datas ? dataConfig = res.datas : dataConfig = [];
          resolve(dataConfig);
        }
      });
    })
  }

  /**
   * 性别转换
   */
  transformSex(par) {
    let aa = '';
    switch (par) {
      case 0:
        aa = '男';
        break;
      case 1:
        aa = '女';
        break;
    }
    return aa;
  }

  /**
   * 岗位类型转换
   */
  transformPType(par) {
    let bb = '';
    switch (par) {
      case 0:
        bb = '专技';
        break;
      case 1:
        bb = '工勤';
        break;
      case 2:
        bb = '管理';
        break;
    }
    return bb;
  }

  /**
   * 判断是否选择
   */
  checkDelete(): void {
    // tslint:disable-next-line:max-line-length
    this.canDelete = !(OtherUtil.deleteIds(this.mapOfCheckedId).length > 0 || OtherUtil.deleteIds(this.studentMapOfCheckedId).length > 0 || OtherUtil.deleteIds(this.classMapOfCheckedId).length > 0);
  }

  /**
   * 当前选择
   * @param num:1-教师，2-学生
   */
  currentPageDataChange($event, num): void {
    if (num === 1) {
      this.displayList = $event;
    } else if (num === 2) {
      this.studentDisplayList = $event;
    } else if (num === 3) {
      this.classDisplayList = $event;
    }
    this.refreshStatus(num);
  }

  /**
   * 刷新选择
   * @param num:1-教师，2-学生
   */
  refreshStatus(num): void {
    if (num === 1) {
      const {checkBool, inNateBool} = this.dealBoolJudge(this.displayList, this.mapOfCheckedId);
      this.isAllCheck = checkBool;
      this.isIndeterminate = inNateBool;
    } else if (num === 2) {
      const {checkBool, inNateBool} = this.dealBoolJudge(this.studentDisplayList, this.studentMapOfCheckedId);
      this.studentIsAllCheck = checkBool;
      this.studentIsIndeterminate = inNateBool;
    } else if (num === 3) {
      const {checkBool, inNateBool} = this.dealBoolJudge(this.classDisplayList, this.classMapOfCheckedId);
      this.classIsAllCheck = checkBool;
      this.classIsIndeterminate = inNateBool;
    }
    this.checkDelete();
  }

  dealBoolJudge(arrConfig: Array<any>, mapConfig: { [key: string]: boolean }): { [key: string]: boolean } {
    let checkBool: boolean = false;
    let inNateBool: boolean = false;
    checkBool = arrConfig.every(item => mapConfig[item.id]);
    inNateBool = arrConfig.some(item => mapConfig[item.id]) && !checkBool;
    let boolObj;
    return boolObj = {
      checkBool,
      inNateBool
    }
  }

  /**
   * 选择所有
   */
  checkAll(value: boolean, num): void {
    if (num === 1) {
      this.dealComFilter(this.displayList, this.mapOfCheckedId, value);
    } else if (num === 2) {
      this.dealComFilter(this.studentDisplayList, this.studentMapOfCheckedId, value);
    } else if (num === 3) {
      this.dealComFilter(this.classDisplayList, this.classMapOfCheckedId, value);
    }
    this.refreshStatus(num);
  }

  dealComFilter(arrConfig: Array<any>, idsConfig: { [key: string]: boolean }, val: any): void {
    arrConfig.forEach(item => (idsConfig[item.id] = val));
  }

  /* 学生开始*/
  /**
   * 分页查询-学生
   * @param page 分页条件
   */
  pageStudent(page: PageEntity): void {
    this.newsService.studentPage(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.studentList = res.datas.data;
        this.studentPageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  /**
   * 查询学院列表
   */
  collegeList() {
    this.newsService.getCollege().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.filterCollegeArr = res.datas;
      }
    });
  }

  /**
   * 表格过滤-学生
   */
  filterSomeStu(event, flag) {
    if (event.length > 0) {
      this.studentPageInfo.first = 1;
      this.studentPageInfo.filters[flag] = {
        matchMode: 'in',
        value: event.join(','),
        whereType: FilterTypeEnum.AND
      };
    } else {
      this.studentPageInfo.filters[flag] = undefined;
    }
    this.pageStudent(this.studentPageInfo);
  }

  /**
   * 专业，班级搜索
   * @param filed 字段：学院，专业
   * @param value 输入的值
   */
  titleSearch(filed: string, value: string): void {
    this.studentPageInfo.filters[filed] = PaginationUtils.filters(value, FilterEnum.CONTAINS);
    this.pageStudent(this.studentPageInfo);
  }

  /**
   * 重置
   * @param field 字段
   */
  resetFilter(field: string): void {
    delete this.studentPageInfo.filters[field];
    this.pageStudent(this.studentPageInfo);
  }

  /**
   * 学生类型转换
   */
  transformType(par) {
    let bb = '';
    switch (par) {
      case 0:
        bb = '本科生';
        break;
      case 1:
        bb = '研究生';
        break;
      case 2:
        bb = '博士生';
        break;
    }
    return bb;
  }

  /**
   * 学生类型转换
   * @param num:1-老师，2-学生，3-班级
   */
  tabMoadalClick(num) {
    this.addNews.customType = num;
    switch (num) {
      case 1:
        // 老师初始状态置空
        this.mapOfCheckedId = {};
        this.isAllCheck = false;
        // 初始查询发布消息老师数据
        this.pageTeacher(this.teacherPageInfo);
        break;
      case 2:
        // 学生选择初始状态置空
        this.studentMapOfCheckedId = {};
        this.studentIsAllCheck = false;
        this.collegeList();
        this.pageStudent(this.studentPageInfo);
        break;
      case 3:
        // 班级选择初始状态置空
        this.classMapOfCheckedId = {};
        this.classIsAllCheck = false;
        this.getClassTree();// 获取学院树结构数据
        break;
      default:
        break
    }
  }

  /*班级*/
  /**
   * 分页查询-学生
   * @param page 分页条件
   */
  pageClass(page: PageEntity): void {
    this.newsService.classPage(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        (res.datas && res.datas.data.length) ? this.classList = res.datas.data : this.classList = [];
        this.classIsAllCheck = false;
        this.classIsIndeterminate = false;
        this.classPageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  /**
   * 分页查询-学院专业树
   */
  getClassTree(): void {
    this.newsService.getTree().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        (res.datas && res.datas.length) ? this.treeNodes = res.datas : this.treeNodes = [];
      }
    });
  }

  /**
   * 选择专业分类点击
   * @param event 数据
   */
  nzEvent(event) {
    this.majorId = event.node.key;
    this.currentType = event.node.origin.value;
    this.dealPage();
    this.pageClass(this.classPageInfo);
  }

  dealPage(): void {
    if (this.currentType) {
      if (this.currentType === '0') {
        // 学院
        this.classPageInfo.filters['collageId'.toString()] = {
          matchMode: FilterEnum.EQUALS,
          value: this.majorId,
          whereType: FilterTypeEnum.AND
        };
        delete this.classPageInfo.filters['majorId'.toString()];
      } else {
        // 专业
        this.classPageInfo.filters['majorId'.toString()] = {
          matchMode: FilterEnum.EQUALS,
          value: this.majorId,
          whereType: FilterTypeEnum.AND
        };
        delete this.classPageInfo.filters['collageId'.toString()];
      }
    }
    this.dealPageLogic(this.classPageInfo, 'name', this.className, FilterEnum.CONTAINS, FilterTypeEnum.AND);
  }

  /**
   * 进行搜索
   */
  searchClass(): void {
    this.classPageInfo = new PageEntity();
    this.dealPage();
    this.pageClass(this.classPageInfo);
  }

  /**
   * 点击重置
   */
  resetSearchClass() {
    this.className = '';
    delete this.classPageInfo.filters['name'.toString()];
    this.pageClass(this.classPageInfo);
  }

  /**
   * 删除
   */
  delete() {
    const arr: Array<string> = this.dealArrLogic();
    this.newsService.sysDelete(arr).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.pageInfo.first = 1;
        this.page(this.pageInfo);
        this.isCkecked = false;
      }
    });
  }

  /**
   * 获取id数组复用逻辑
   * @param idObj:id对象集
   */
  dealIdArrLogic(idObj: any): Array<string> {
    const arr = [];
    for (const val in idObj) {
      if (idObj[val]) {
        arr[arr.length] = val;
      }
    }
    return arr;
  }

  /**
   * 确定所选教师/学生/班级数据
   */
  sureCheck(): void {
    let arr: Array<string | undefined> = [];
    if (this.addNews.customType === 1) {
      // 教师
      arr = this.dealIdArrLogic(this.mapOfCheckedId);
    } else if (this.addNews.customType === 2) {
      // 学生
      arr = this.dealIdArrLogic(this.studentMapOfCheckedId);
    } else if (this.addNews.customType === 3) {
      // 班级
      arr = this.dealIdArrLogic(this.classMapOfCheckedId);
    }
    this.addNews.receiverIds = arr;// 存入参数数组
    const request = {
      customType: this.addNews.customType,
      receiverIds: arr
    };
    // 查询选择的names数据
    this.newsService.getNames(request).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        res.datas ? this.checkNames = res.datas : this.checkNames = '';
      }
    });
    this.isShowModalContent = false;
  }

  /**
   * 取消所选
   */
  sureCancel() {
    this.isShowModalContent = false;
  }

  /**
   * 发布消息
   */
  sendInfo() {
    if (!this.addNews.content || this.addNews.content === '') {
      this.message.error('内容不能为空！');
      return;
    }
    this.newsService.sendInfoFn(this.addNews).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.tabClick(0);
        this.isModalSend = false;
      }
    });
  }
}

class SendInfoParam {
  content: string;
  customType: number;
  receiverIds: Array<string | any>;
  receiverType: string | undefined;
  eject: string | undefined;
  grade: string | undefined;

}

/**
 * 查看详细信息
 */
class LookView {
  senderName: string | undefined;
  gmtCreate: string | undefined;
  content: string | undefined;
  title: string | undefined;
  hasRead: number;
}
